package com.example.demo.solution;

import java.util.Stack;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年08⽉16⽇<br/>
 * @version：v1.0.0<br>
 */
public class FindMaxValue {

    public static void main(String[] args) {
        int[] heghtInts = new int[7];
        heghtInts[0] = 456;
        heghtInts[1] = 2;
        heghtInts[2] = 5;
        heghtInts[3] = 4;
        heghtInts[4] = 5;
        heghtInts[5] = 2;
        heghtInts[6] = 6;
        int largestValue = findLargestValue(heghtInts);
        System.out.println(largestValue);
    }

    public static int findLargestValue(int[] array){
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < array.length; i++) {
            if(stack.isEmpty()){
                stack.push(array[i]);
            }else {
                Integer peek = stack.peek();
                if(array[i] > peek){
                    stack.pop();
                    stack.push(array[i]);
                }
            }
        }
        Integer returnVal = stack.pop();
        int size = stack.size();
        System.out.println("stack size = " + size);
        return returnVal;
    }
}
